// Parts Copyright 2008 Newgrove Consultants Limited
// Modified By John Diss 
// From original work:
//
using System;
using System.IO;
using GeoAPI.Geometries;
using MsSqlSpatial.UserDefinedTypes;

namespace MsSqlSpatial.IO
{
    public static class MsSqlSpatialReader
    {
        //public static IGeometry Read(Stream stream)
        //{
        //    BinaryReader reader = null;
        //    ByteOrder byteOrder = (ByteOrder)stream.ReadByte();
        //    try
        //    {
        //        if (byteOrder == ByteOrder.BigEndian)
        //            reader = new BEBinaryReader(stream);
        //        else reader = new BinaryReader(stream);
        //        IGeometry geometry = Read(reader);
        //        int srid = -1;
        //        try
        //        {
        //            srid = reader.ReadInt32();
        //        }
        //        catch { }
        //        if (srid == 0)
        //            srid = -1;
        //        geometry.SRID = srid;
        //        return geometry;
        //    }
        //    finally
        //    {
        //        if (reader != null)
        //            reader.Close();
        //    }
        //}

        //public static IGeometry<TCoordinate> Read<TCoordinate>(Stream stream, IGeometryFactory<TCoordinate> geometryFactory)
        //    where TCoordinate : ICoordinate<TCoordinate>, IEquatable<TCoordinate>, IComparable<TCoordinate>, IComputable<Double, TCoordinate>, IConvertible
        //{
        //    return (IGeometry<TCoordinate>)Read(stream, (IGeometryFactory)geometryFactory);
        //}


        //public static IGeometry<TCoordinate> Read<TCoordinate>(byte[] wkb, IGeometryFactory<TCoordinate> geometryFactory)
        //     where TCoordinate : ICoordinate<TCoordinate>, IEquatable<TCoordinate>, IComparable<TCoordinate>, IComputable<Double, TCoordinate>, IConvertible
        //{
        //    return (IGeometry<TCoordinate>)Read(wkb, (IGeometryFactory)geometryFactory);
        //}

        public static IGeometry Read(byte[] wkb, IGeometryFactory geometryFactory)
        {
            using (var ms = new MemoryStream(wkb))
                return Read(ms, geometryFactory);
        }

        public static IGeometry Read(Stream stream, IGeometryFactory geometryFactory)
        {
            IGeometry geom;
            using (var br = new BinaryReader(stream))
            {
                geom = Global.Services.GeometryFactory.WkbReader.Read(br);

                int? srid = null;
                if (stream.Position < stream.Length)
                {
                    try
                    {
                        srid = br.ReadInt32();
                    }
                    catch (Exception ex)
                    {
                        SqlUtility.WriteExceptionToPipe(ex);
                    }
                }

                if (srid.HasValue)
                    geom = GeometryUtilities.SetSrid(geom, srid.Value);
            }
            return geom;
        }
    }
}